import 'dart:convert';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:zego_faceunity_plugin/zego_faceunity_plugin.dart';

// 即构相芯美颜
class ZegoFace {
  /// Private constructor
  ZegoFace._internal() {
    SharedPreferences.getInstance().then((prefs) {
      final str = prefs.getString('faceparam');
      if (str == null) return;
      final arr = jsonDecode(str);
      if (arr is List<Map<String, dynamic>> && arr.length > 0) {
        beautyParamList.clear();
        beautyParamList.addAll(arr);
      }
    });
  }

  /// Engine singleton instance
  static final ZegoFace instance = ZegoFace._internal();

  // 美颜默认属性
  final List<Map<String, dynamic>> beautyParamList = [
    {'name': '美白', 'value': 1.0, 'min': 0.0, 'max': 2.0},
    {'name': '红润', 'value': 0.5, 'min': 0.0, 'max': 2.0},
    {'name': '磨皮', 'value': 4.2, 'min': 0.0, 'max': 6.0},
    {'name': '大眼', 'value': 0.4, 'min': 0.0, 'max': 1.0},
    {'name': '瘦脸', 'value': 0.0, 'min': 0.0, 'max': 1.0},
    {'name': 'V脸', 'value': 0.5, 'min': 0.0, 'max': 1.0},
    {'name': '窄脸', 'value': 0.0, 'min': 0.0, 'max': 1.0},
    {'name': '小脸', 'value': 0.0, 'min': 0.0, 'max': 1.0},
    {'name': '下巴', 'value': 0.3, 'min': 0.0, 'max': 1.0},
    {'name': '额头', 'value': 0.3, 'min': 0.0, 'max': 1.0},
    {'name': '鼻子', 'value': 0.5, 'min': 0.0, 'max': 1.0},
    {'name': '嘴型', 'value': 0.4, 'min': 0.0, 'max': 1.0},
  ];

  // 设置美颜属性
  void setBeautyOption(int index) {
    print('cureent value: ${beautyParamList[index]['value']}');
    switch (beautyParamList[index]['name']) {
      case '美白':
        ZegoFaceunityPlugin.instance
            .setBeautyOption(faceWhiten: beautyParamList[index]['value']);
        break;
      case '红润':
        ZegoFaceunityPlugin.instance
            .setBeautyOption(faceRed: beautyParamList[index]['value']);
        break;
      case '磨皮':
        ZegoFaceunityPlugin.instance
            .setBeautyOption(faceBlur: beautyParamList[index]['value']);
        break;
      case '大眼':
        ZegoFaceunityPlugin.instance
            .setBeautyOption(eyeEnlarging: beautyParamList[index]['value']);
        break;
      case '瘦脸':
        ZegoFaceunityPlugin.instance
            .setBeautyOption(cheekThinning: beautyParamList[index]['value']);
        break;
      case 'V脸':
        ZegoFaceunityPlugin.instance
            .setBeautyOption(cheekV: beautyParamList[index]['value']);
        break;
      case '窄脸':
        ZegoFaceunityPlugin.instance
            .setBeautyOption(cheekNarrow: beautyParamList[index]['value']);
        break;
      case '小脸':
        ZegoFaceunityPlugin.instance
            .setBeautyOption(cheekSmall: beautyParamList[index]['value']);
        break;
      case '下巴':
        ZegoFaceunityPlugin.instance
            .setBeautyOption(chinLevel: beautyParamList[index]['value']);
        break;
      case '额头':
        ZegoFaceunityPlugin.instance
            .setBeautyOption(foreHeadLevel: beautyParamList[index]['value']);
        break;
      case '鼻子':
        ZegoFaceunityPlugin.instance
            .setBeautyOption(noseLevel: beautyParamList[index]['value']);
        break;
      case '嘴型':
        ZegoFaceunityPlugin.instance
            .setBeautyOption(mouthLevel: beautyParamList[index]['value']);
        break;
    }
  }

  // 推出美颜设置面板
  show(BuildContext context) {
    showModalBottomSheet<void>(
      barrierColor: Color.fromRGBO(0, 0, 0, 0.1),
      backgroundColor: Colors.transparent,
      context: context,
      builder: (BuildContext context) {
        return StatefulBuilder(
          builder: (context1, setBottomSheetState) {
            return Container(
              height: 200.0,
              padding: EdgeInsets.all(20),
              color: Color.fromRGBO(0, 0, 0, 0.8),
              child: ListView.builder(
                itemCount: beautyParamList.length,
                itemBuilder: (BuildContext context, int index) {
                  return Row(
                    children: [
                      Text(
                        beautyParamList[index]['name'],
                        style: TextStyle(color: Colors.white),
                      ),
                      Expanded(
                        child: CupertinoSlider(
                          value: beautyParamList[index]['value'],
                          min: beautyParamList[index]['min'],
                          max: beautyParamList[index]['max'],
                          divisions: 20,
                          onChanged: (value) {
                            setBottomSheetState(() {
                              beautyParamList[index]['value'] = value;
                              setBeautyOption(index);
                              SharedPreferences.getInstance().then((prefs) {
                                final str = jsonEncode(beautyParamList);
                                prefs.setString('faceparam', str);
                              });
                            });
                          },
                        ),
                      )
                    ],
                  );
                },
              ),
            );
          },
        );
      },
    );
  }
}
